﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using School.Data;
using School.Data.Models;

namespace School.Controllers
{
    public class SemesterStudentCoursesController : Controller
    {
        private SchoolDbContext db = new SchoolDbContext();

        // GET: SemesterStudentCourses
        public ActionResult Index()
        {
            var semesterStudentsCourses = db.SemesterStudentsCourses.Include(s => s.Course).Include(s => s.Semester).Include(s => s.Student);
            return View(semesterStudentsCourses.ToList());
        }

        // GET: SemesterStudentCourses/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SemesterStudentCourse semesterStudentCourse = db.SemesterStudentsCourses.Find(id);
            if (semesterStudentCourse == null)
            {
                return HttpNotFound();
            }
            return View(semesterStudentCourse);
        }

        // GET: SemesterStudentCourses/Create
        public ActionResult Create()
        {
            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name");
            ViewBag.SemesterId = new SelectList(db.Semesters, "SemesterId", "SemesterId");
            ViewBag.StudentId = new SelectList(db.Students, "StudentId", "FirstName");
            return View();
        }

        // POST: SemesterStudentCourses/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "SemesterStudentCourseId,SemesterId,StudentId,CourseId")] SemesterStudentCourse semesterStudentCourse)
        {
            if (ModelState.IsValid)
            {
                db.SemesterStudentsCourses.Add(semesterStudentCourse);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", semesterStudentCourse.CourseId);
            ViewBag.SemesterId = new SelectList(db.Semesters, "SemesterId", "SemesterId", semesterStudentCourse.SemesterId);
            ViewBag.StudentId = new SelectList(db.Students, "StudentId", "FirstName", semesterStudentCourse.StudentId);
            return View(semesterStudentCourse);
        }

        // GET: SemesterStudentCourses/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SemesterStudentCourse semesterStudentCourse = db.SemesterStudentsCourses.Find(id);
            if (semesterStudentCourse == null)
            {
                return HttpNotFound();
            }
            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", semesterStudentCourse.CourseId);
            ViewBag.SemesterId = new SelectList(db.Semesters, "SemesterId", "SemesterId", semesterStudentCourse.SemesterId);
            ViewBag.StudentId = new SelectList(db.Students, "StudentId", "FirstName", semesterStudentCourse.StudentId);
            return View(semesterStudentCourse);
        }

        // POST: SemesterStudentCourses/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "SemesterStudentCourseId,SemesterId,StudentId,CourseId")] SemesterStudentCourse semesterStudentCourse)
        {
            if (ModelState.IsValid)
            {
                db.Entry(semesterStudentCourse).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", semesterStudentCourse.CourseId);
            ViewBag.SemesterId = new SelectList(db.Semesters, "SemesterId", "SemesterId", semesterStudentCourse.SemesterId);
            ViewBag.StudentId = new SelectList(db.Students, "StudentId", "FirstName", semesterStudentCourse.StudentId);
            return View(semesterStudentCourse);
        }

        // GET: SemesterStudentCourses/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SemesterStudentCourse semesterStudentCourse = db.SemesterStudentsCourses.Find(id);
            if (semesterStudentCourse == null)
            {
                return HttpNotFound();
            }
            return View(semesterStudentCourse);
        }

        // POST: SemesterStudentCourses/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            SemesterStudentCourse semesterStudentCourse = db.SemesterStudentsCourses.Find(id);
            db.SemesterStudentsCourses.Remove(semesterStudentCourse);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
